<template>
  <div class="resource-bar">
    <div 
      v-for="resource in displayResources" 
      :key="resource.id"
      class="resource-item"
      :class="{ 'resource-low': resource.isLow, 'resource-critical': resource.isCritical }"
      :title="resource.tooltip"
    >
      <div class="resource-icon">{{ resource.icon }}</div>
      <div class="resource-info">
        <div class="resource-amount">{{ formatNumber(resource.amount) }}</div>
        <div class="resource-production" v-if="resource.production !== 0">
          <span :class="resource.production > 0 ? 'positive' : 'negative'">
            {{ resource.production > 0 ? '+' : '' }}{{ formatNumber(resource.production) }}/s
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useResourcesStore } from '@/stores'

const resourcesStore = useResourcesStore()

// 主要显示的资源
const mainResourceIds = ['energy', 'metals', 'crystals', 'credits', 'research_points', 'influence']

// 资源图标映射
const resourceIcons: Record<string, string> = {
  energy: '⚡',
  metals: '🔩',
  crystals: '💎',
  rare_metals: '🌟',
  credits: '💰',
  research_points: '🔬',
  influence: '👑',
  food: '🌾',
  population: '👥'
}

// 显示的资源数据
const displayResources = computed(() => {
  return mainResourceIds.map(resourceId => {
    const amount = resourcesStore.getResource(resourceId)
    const production = resourcesStore.getResourceProduction(resourceId)
    const storage = resourcesStore.getResourceStorage(resourceId)
    
    // 计算警告状态
    const isLow = storage > 0 && amount < storage * 0.2
    const isCritical = storage > 0 && amount < storage * 0.1
    
    return {
      id: resourceId,
      icon: resourceIcons[resourceId] || '❓',
      amount,
      production,
      storage,
      isLow,
      isCritical,
      tooltip: generateTooltip(resourceId, amount, production, storage)
    }
  })
})

// 生成提示信息
function generateTooltip(resourceId: string, amount: number, production: number, storage: number): string {
  const resourceName = resourcesStore.getResourceName(resourceId)
  let tooltip = `${resourceName}: ${formatNumber(amount)}`
  
  if (storage > 0) {
    tooltip += ` / ${formatNumber(storage)}`
  }
  
  if (production !== 0) {
    tooltip += `\n生产: ${production > 0 ? '+' : ''}${formatNumber(production)}/秒`
  }
  
  if (production > 0 && storage > 0 && amount < storage) {
    const timeToFull = (storage - amount) / production
    tooltip += `\n存储满需要: ${formatTime(timeToFull)}`
  } else if (production < 0 && amount > 0) {
    const timeToEmpty = amount / Math.abs(production)
    tooltip += `\n耗尽时间: ${formatTime(timeToEmpty)}`
  }
  
  return tooltip
}

// 格式化数字
function formatNumber(num: number): string {
  if (num >= 1000000000) {
    return (num / 1000000000).toFixed(1) + 'B'
  } else if (num >= 1000000) {
    return (num / 1000000).toFixed(1) + 'M'
  } else if (num >= 1000) {
    return (num / 1000).toFixed(1) + 'K'
  } else {
    return Math.floor(num).toString()
  }
}

// 格式化时间
function formatTime(seconds: number): string {
  if (seconds < 60) {
    return `${Math.floor(seconds)}秒`
  } else if (seconds < 3600) {
    return `${Math.floor(seconds / 60)}分钟`
  } else if (seconds < 86400) {
    return `${Math.floor(seconds / 3600)}小时`
  } else {
    return `${Math.floor(seconds / 86400)}天`
  }
}
</script>

<style scoped>
.resource-bar {
  display: flex;
  gap: 1rem;
  align-items: center;
  padding: 0.5rem;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 8px;
  backdrop-filter: blur(5px);
}

.resource-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.5rem;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
  cursor: help;
}

.resource-item:hover {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(0, 212, 255, 0.5);
  transform: translateY(-1px);
}

.resource-item.resource-low {
  border-color: rgba(255, 165, 0, 0.6);
  background: rgba(255, 165, 0, 0.1);
}

.resource-item.resource-critical {
  border-color: rgba(255, 0, 0, 0.6);
  background: rgba(255, 0, 0, 0.1);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

.resource-icon {
  font-size: 1.2rem;
  min-width: 20px;
  text-align: center;
}

.resource-info {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  min-width: 60px;
}

.resource-amount {
  font-weight: bold;
  font-size: 0.9rem;
  color: #ffffff;
}

.resource-production {
  font-size: 0.7rem;
  margin-top: 1px;
}

.resource-production .positive {
  color: #4ade80;
}

.resource-production .negative {
  color: #f87171;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .resource-bar {
    gap: 0.5rem;
  }
  
  .resource-item {
    padding: 0.2rem 0.4rem;
  }
  
  .resource-info {
    min-width: 50px;
  }
}

@media (max-width: 768px) {
  .resource-bar {
    flex-wrap: wrap;
    gap: 0.3rem;
  }
  
  .resource-item {
    padding: 0.15rem 0.3rem;
  }
  
  .resource-icon {
    font-size: 1rem;
  }
  
  .resource-amount {
    font-size: 0.8rem;
  }
  
  .resource-production {
    font-size: 0.6rem;
  }
}
</style>